# _*_ coding: utf-8 _*_
from collections import defaultdict
from Post import Post
import datetime
import json
class Blog:
    def __init__(self,title,posts=None):
        self.title = title
        self.entries = posts if posts is not None else []
    def append(self, post):
        self.entries.append(post)
    def by_tag(self):
        tag_index = defaultdict(list)
        for post in self.entries:
            for tag in post.tags:
                tag_index[tag].append(post.as_dict() )
        return tag_index
    def as_dict(self):
        return dict(
            title=self.title,
            underline= "="*len(self.title),
            entries = [p.as_dict() for p in self.entries]
        )
if __name__ == '__main__':
    travel = Blog("Travel")
    travel.append(
        Post(date=datetime.datetime(2013,11,14,17,25),title="Hard Aground",rst_text="""Some embarrassing revelation""",tags=("#RedRanger", "#Whitby42", "#ICW"))
    )
    with open("../Data/db.emp","a") as json_file:
        json.dump(travel.as_dict(), json_file, indent=4,ensure_ascii = True)
    print json.dumps(travel.as_dict(),indent=4)